﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.Tools
{
    public class HttpClient
    {
        /// <summary>
        /// Cookie，保证登录后，所有访问持有一个Cookie；
        /// </summary>
        static CookieContainer Cookie = new CookieContainer();
        public string token;
        public string appkey;
        public string workcode;
        /// <summary>
        /// HTTP访问
        /// </summary>
        public string AsyncRequest(string url)
        {
            HttpWebRequest httpRequest = HttpWebRequest.Create(url) as HttpWebRequest;
            httpRequest.Method = "POST";
            httpRequest.KeepAlive = false;
            httpRequest.ContentType = "application/json";

            httpRequest.CookieContainer = Cookie;
            httpRequest.Timeout = 1000 * 60 * 60;//10min
            httpRequest.ReadWriteTimeout = 1000 * 60 * 60;

            //using (Stream reqStream = httpRequest.GetRequestStream())
            //{
            //    var bytes = Encoding.UTF8.GetBytes(Content);
            //    reqStream.Write(bytes, 0, bytes.Length);
            //    reqStream.Flush();
            //}
            try
            {
                using (var repStream = httpRequest.GetResponse().GetResponseStream())
                {
                    using (var reader = new StreamReader(repStream))
                    {
                        return ValidateResult(reader.ReadToEnd());
                    }
                }
            }
            catch (Exception er)
            {

                throw new Exception(string.Format("请求错误：" + er.Message));
            }

        }
        internal string AsyncRequest(string esbUrl, string appkey, string workcode)
        {
            HttpWebRequest httpRequest = HttpWebRequest.Create(esbUrl) as HttpWebRequest;
            httpRequest.Method = "GET";
            httpRequest.KeepAlive = false;
            httpRequest.ContentType = "application/json";

            httpRequest.CookieContainer = Cookie;
            httpRequest.Timeout = 1000 * 60 * 60;//10min
            httpRequest.ReadWriteTimeout = 1000 * 60 * 60;
            httpRequest.Headers.Add("appkey", appkey);
            httpRequest.Headers.Add("workcode", workcode);
            //using (Stream reqStream = httpRequest.GetRequestStream())
            //{
            //    var bytes = Encoding.UTF8.GetBytes(json);
            //    reqStream.Write(bytes, 0, bytes.Length);
            //    reqStream.Flush();
            //}
            try
            {
                using (var repStream = httpRequest.GetResponse().GetResponseStream())
                {
                    using (var reader = new StreamReader(repStream))
                    {
                        return ValidateResult(reader.ReadToEnd());
                    }
                }
            }
            catch (Exception er)
            {

                throw new Exception(string.Format("请求错误：" + er.Message));
            }
        }

        public string AsyncRequest(string url, string json, string appkey, string workcode, int requestId)
        {
            HttpWebRequest httpRequest = HttpWebRequest.Create(url) as HttpWebRequest;
            httpRequest.Method = "POST";
            httpRequest.KeepAlive = false;
            httpRequest.ContentType = "application/json";

            httpRequest.CookieContainer = Cookie;
            httpRequest.Timeout = 1000 * 60 * 60;//10min
            httpRequest.ReadWriteTimeout = 1000 * 60 * 60;
            httpRequest.Headers.Add("appkey", appkey);
            httpRequest.Headers.Add("workcode", workcode);

            using (Stream reqStream = httpRequest.GetRequestStream())
            {
                var bytes = Encoding.UTF8.GetBytes(json);
                reqStream.Write(bytes, 0, bytes.Length);
                reqStream.Flush();
            }
            try
            {
                using (var repStream = httpRequest.GetResponse().GetResponseStream())
                {
                    using (var reader = new StreamReader(repStream))
                    {
                        return ValidateResult(reader.ReadToEnd());
                    }
                }
            }
            catch (Exception er)
            {

                throw new Exception(string.Format("请求错误：" + er.Message));
            }
        }

        private string ValidateResult(string responseText)
        {
            if (responseText.StartsWith("response_error:"))
            {
                return responseText.TrimStart("response_error:".ToCharArray());
            }
            return responseText;
        }
    }
}
